package javaer.starter.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import javaer.starter.constant.StorePathEnum;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

/**
 * Freemarker工具
 *
 * @author yangzhen
 * @date 2023/6/15 11:30
 * @since 1.0.0
 */
@Slf4j
public class FreemarkerUtil {

    /**
     * classpath
     */
    private static Configuration classPathConfiguration;

    /**
     * 指定目录
     */
    private static Configuration configuration;

    public static Configuration getClassPathConfiguration() throws IOException {
        if (classPathConfiguration != null) {
            return classPathConfiguration;
        }

        synchronized (FreemarkerUtil.class) {
            if (classPathConfiguration != null) {
                return classPathConfiguration;
            }

            classPathConfiguration = new Configuration(Configuration.VERSION_2_3_30);
            classPathConfiguration.setDefaultEncoding("utf-8");

            //加载模板文件(模板的路径)  这个操作在打包成jar之后会报错
//            File file = ResourceUtils.getFile("classpath:freemarker");
//            configuration.setDirectoryForTemplateLoading(file);
            classPathConfiguration.setClassForTemplateLoading(FreemarkerUtil.class, "/freemarker");

            return classPathConfiguration;
        }
    }

    public static Configuration getConfiguration() throws IOException {
        if (configuration != null) {
            return configuration;
        }

        synchronized (FreemarkerUtil.class) {
            if (configuration != null) {
                return configuration;
            }

            configuration = new Configuration(Configuration.VERSION_2_3_30);
            configuration.setDefaultEncoding("utf-8");
            configuration.setDirectoryForTemplateLoading(new File(StorePathEnum.TEMPLATE_PATH.getDirectory()));
            return configuration;
        }
    }

    public static void writeFromClasspath(String path, File file, Object param) throws IOException, TemplateException {
        Configuration configuration = FreemarkerUtil.getClassPathConfiguration();
        write(path, file, param, configuration);
    }

    public static void writeFromDynamic(String path, File file, Object param) throws IOException, TemplateException {
        log.info("模板路径 [{}]", path);
        log.info("文件路径 [{}]", file.getAbsolutePath());
        Configuration configuration = FreemarkerUtil.getConfiguration();
        write(path, file, param, configuration);
    }

    public static void write(String path, File file, Object param, Configuration configuration) throws IOException, TemplateException {
        Template template = configuration.getTemplate(path);
        Writer out = new FileWriter(file);
        template.process(param, out);
        out.flush();
        out.close();
    }

    public static String writeMemory(String path, Object param, Configuration configuration) throws IOException, TemplateException {
        try (StringWriter writer = new StringWriter()) {
            Template template = configuration.getTemplate(path);
            template.process(param, writer);
            writer.flush();
            return writer.toString();
        }
    }

    public static void main(String[] args) {

    }

}
